#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include <functional>
#include<stdlib.h>

using task_t = std::function<void()>;

static int number = 0;

void task_1()
{
  std::cout<<"我是任务1"<<std::endl;
}

void task_2()
{
  std::cout<<"我是任务2"<<std::endl;
}

void task_3()
{
  std::cout<<"我是任务3"<<std::endl;
}

class Task
{
public:
  Task()
  {
    Task_insert(task_1);
    Task_insert(task_2);
    Task_insert(task_3);
  }

  void Task_insert(task_t t)
  {
    hash[number++] = t;
  }
  void Task_action(int number)
  {
    if (hash.find(number) == hash.end())
    {
      std::cout << "not find task" << std::endl;
      return;
    }
    hash[number]();
  }

  int Task_select()
  {
    return (int)rand()%number;
  }

private:
  std::unordered_map<int, task_t> hash;
};

Task task;


void work()
{
    while (true)
    {
        int task_number = 0;
        int n = read(0, &task_number, sizeof(task_number));
        if (n == sizeof(task_number))
        {
            task.Task_action(task_number);
        }
        else if (n == 0)
        {
            break;
        }
        else
        {
            //读取失败，或者是等待状态
        }
    }
}